package cn.hyxy.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.Iterator;

public class Demo03_CountSortMR extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("usage ...");
            return -1;
        }
        // 7:声明Job
        Configuration config = getConf();
        FileSystem fs = FileSystem.get(config);
        Path path = new Path(args[1]);
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        // 8:Job
        Job job = Job.getInstance(config, "个数排序");
        //!!!!!!!!!!!!!!!!!! 本地
        job.setJarByClass(getClass());
        
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(Text.class);
        //
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, path);
        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int code = ToolRunner.run(new Demo03_CountSortMR(), args);
        System.exit(code);
    }

    //1:开发Mapper类
    public static class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
        private LongWritable key2 = new LongWritable(0);//k2
        private Text value2 = new Text();//v2

        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //读取到每一行，执行一次map方法， value=Jack 4
            String[] strs = value.toString().split("\\s+");
            key2.set(Long.parseLong(strs[1]));
            value2.set(strs[0]);
            context.write(key2, value2);
        }
    }

    //{1,[Jack,Mary,Rose,Alex]

    public static class MyReducer extends Reducer<LongWritable, Text, Text, LongWritable> {
        @Override
        public void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
           for(Text tt:values){
               context.write(tt,key);
           }
        }
    }
    
/*    public static class MyReducer extends Reducer<LongWritable, Text, Text, LongWritable> {
    	static int flag=0;  //
    	
        @Override
        public void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
           
        	flag+=1;
        	int i=0;
        	
        	Iterator iterator=values.iterator();
        	while (iterator.hasNext()) {
        		Text text=(Text) iterator.next();
        		System.out.println("--------Text:"+text);
				context.write(text, key);
				System.out.println("---------Key:"+key);
				i++;
				System.out.println("i:"+i+"============  flag:"+flag);				
				
			}
        	
        	*//** 原始数据：
             * Jack	3
        		Json	2
        		Mary	1
        		Mike	1
        		Rose	3
             *//*
            
           *//**
            * 执行完Mapper后：
            *  {1,[Mary,Mike]
            *  {2,[Json]}
            *  {3,[Rose,Jack]}
            *
            * 接下来执行Reduce
            *//*
        	
        	*//**上面的输出结果：
        	 *  --------Text:Mike
				---------Key:1
				i:1============  flag:1
				--------Text:Mary
				---------Key:1
				i:2============  flag:1
				--------Text:Json
				---------Key:2
				i:1============  flag:2
				--------Text:Rose
				---------Key:3
				i:1============  flag:3
				--------Text:Jack
				---------Key:3
				i:2============  flag:3
        	 *//*
        	

        }
    }*/

 
}



